package cn.lyl.order.service.impl;

import cn.lyl.basic.util.LoginContext;
import cn.lyl.basic.util.PageList;
import cn.lyl.order.domain.OrderPetAcquisition;
import cn.lyl.order.dto.OrderDto;
import cn.lyl.order.mapper.OrderPetAcquisitionMapper;
import cn.lyl.order.query.OrderPetAcquisitionQuery;
import cn.lyl.order.service.IOrderPetAcquisitionService;
import cn.lyl.basic.service.impl.BaseServiceImpl;
import cn.lyl.org.domain.Employee;
import cn.lyl.org.domain.Shop;
import cn.lyl.org.mapper.EmployeeMapper;
import cn.lyl.org.mapper.ShopMapper;
import cn.lyl.pet.domain.Pet;
import cn.lyl.pet.domain.SearchMasterMsg;
import cn.lyl.pet.domain.SearchMasterMsgAuditLog;
import cn.lyl.pet.mapper.PetMapper;
import cn.lyl.pet.mapper.SearchMasterMsgMapper;
import cn.lyl.pet.query.SearchMasterMsgAuditLogQuery;
import cn.lyl.user.domain.Logininfo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLoginfoId(logininfo.getId());
        if(employee.getShop_id()!=null){//店长或者店员
            Shop shop = shopMapper.findOne(employee.getShop_id());
            if(shop.getAdminId() != employee.getId()){//店员
                query.setEmpId(employee.getId());
            }
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }


    @Override
    public void cancel(Long id) {
        //1.置空shopId 放入寻主池 state=3 shopId=null
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //2.修改订单状态为-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        //订单确认：处理人去用户那里吧宠物接回来- 平台就会多一个宠物，需要宠物
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();//最终成交价
        Integer paytype = orderDto.getPaytype();//支付方式
        //1.生成宠物对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderId);
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        Pet pet = creatPet(msg,money);
        petMapper.add(pet);
        //2.修改订单状态为1，并且要修改金额，最终成交价
        order.setState(1);
        order.setPrice(money);
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);
        //修改寻主消息的状态：state=2
        msg.setState(2);//寻主消息完成
        searchMasterMsgMapper.update(msg);

    }

    private Pet creatPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        //pet.setUserId(msg.getUserId());这个是领养人
        pet.setSearchMasterMsgId(msg.getId());
        return pet;

    }
}
